<?php
/**
 * Composer依赖修复测试文件
 * 用于测试替代包的功能是否正常工作
 */

// 设置错误报告
error_reporting(E_ALL);
ini_set('display_errors', 1);

// 自动加载
require_once __DIR__ . '/../vendor/autoload.php';

use app\common\library\CodeHighlighter;
use app\common\library\SimpleReflection;
use app\common\library\SimpleKaTeX;
use app\common\library\MathFormulaProcessor;

class ComposerFixTest
{
    public function runAllTests()
    {
        echo "<!DOCTYPE html>\n";
        echo "<html>\n";
        echo "<head>\n";
        echo "<meta charset='utf-8'>\n";
        echo "<title>Composer依赖修复测试</title>\n";
        echo "<style>\n";
        echo "body { font-family: Arial, sans-serif; margin: 20px; background: #f5f5f5; }\n";
        echo ".container { max-width: 1000px; margin: 0 auto; background: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }\n";
        echo "h1, h2 { color: #333; }\n";
        echo "pre { background: #f8f8f8; padding: 10px; border-left: 3px solid #ddd; overflow-x: auto; }\n";
        echo ".success { color: green; }\n";
        echo ".error { color: red; }\n";
        echo ".info { color: blue; }\n";
        echo "hr { border: 0; height: 1px; background: #eee; margin: 20px 0; }\n";
        echo "</style>\n";
        echo SimpleKaTeX::generateCSS(); // 添加数学公式样式
        echo "</head>\n";
        echo "<body>\n";
        echo "<div class='container'>\n";
        echo "<h1>Composer依赖修复测试</h1>\n";
        echo "<p>测试时间: " . date('Y-m-d H:i:s') . "</p>\n";
        echo "<hr>\n";
        
        $this->testCodeHighlighter();
        echo "<hr>\n";
        $this->testSimpleReflection();
        echo "<hr>\n";
        $this->testSimpleKaTeX();
        echo "<hr>\n";
        $this->testMathFormulaProcessor();
        
        echo "</div>\n";
        echo SimpleKaTeX::generateJS(); // 添加JavaScript
        echo "</body>\n";
        echo "</html>\n";
    }
    
    private function testCodeHighlighter()
    {
        echo "<h2>1. 代码高亮测试</h2>\n";
        
        try {
            // 测试PHP代码高亮
            $phpCode = '<?php
class TestClass {
    public function testMethod($param1, $param2 = "default") {
        if ($param1 > 0) {
            return $param1 * $param2;
        }
        return 0;
    }
}
?>';
            
            $highlighted = CodeHighlighter::highlight($phpCode, 'php');
            echo "<p class='success'>✓ PHP代码高亮功能正常</p>\n";
            echo "<pre>" . htmlspecialchars($highlighted) . "</pre>\n";
            
            // 测试JavaScript代码高亮
            $jsCode = 'function testFunction(param1, param2) {
    if (param1 > 0) {
        return param1 * param2;
    }
    return 0;
}';
            
            $highlighted = CodeHighlighter::highlight($jsCode, 'javascript');
            echo "<p class='success'>✓ JavaScript代码高亮功能正常</p>\n";
            
        } catch (Exception $e) {
            echo "<p class='error'>✗ 代码高亮测试失败: " . $e->getMessage() . "</p>\n";
        }
    }
    
    private function testSimpleReflection()
    {
        echo "<h2>2. 简单反射测试</h2>\n";
        
        try {
            // 测试获取当前类的方法
            $methods = SimpleReflection::getClassMethods('ComposerFixTest');
            echo "<p class='success'>✓ 简单反射功能正常</p>\n";
            echo "<p>ComposerFixTest类的方法数量: " . count($methods) . "</p>\n";
            
            // 显示前几个方法
            echo "<pre>";
            foreach (array_slice($methods, 0, 3) as $method) {
                echo "方法: " . $method['name'] . " (";
                echo $method['isPublic'] ? 'public' : '';
                echo $method['isProtected'] ? 'protected' : '';
                echo $method['isPrivate'] ? 'private' : '';
                echo $method['isStatic'] ? ', static' : '';
                echo ")\n";
            }
            echo "</pre>\n";
            
        } catch (Exception $e) {
            echo "<p class='error'>✗ 简单反射测试失败: " . $e->getMessage() . "</p>\n";
        }
    }
    
    private function testSimpleKaTeX()
    {
        echo "<h2>3. 简单KaTeX测试</h2>\n";
        
        try {
            // 测试数学公式处理
            $content = '这是一个包含数学公式的文本：$E = mc^2$ 和 $$\int_{0}^{\infty} e^{-x^2} dx$$';
            $processed = SimpleKaTeX::processFormulas($content);
            echo "<p class='success'>✓ 简单KaTeX功能正常</p>\n";
            echo "<p>处理后的结果:</p>\n";
            echo "<div>" . $processed . "</div>\n";
            
        } catch (Exception $e) {
            echo "<p class='error'>✗ 简单KaTeX测试失败: " . $e->getMessage() . "</p>\n";
        }
    }
    
    private function testMathFormulaProcessor()
    {
        echo "<h2>4. 数学公式处理器测试</h2>\n";
        
        try {
            // 测试数学公式处理
            $content = '二次方程的解为：$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$';
            $processed = MathFormulaProcessor::processFormulas($content);
            echo "<p class='success'>✓ 数学公式处理器功能正常</p>\n";
            echo "<p>处理后的结果:</p>\n";
            echo "<div>" . $processed . "</div>\n";
            
            // 测试CSS生成
            $css = MathFormulaProcessor::getRecommendedCSS();
            echo "<p class='success'>✓ CSS生成功能正常</p>\n";
            
        } catch (Exception $e) {
            echo "<p class='error'>✗ 数学公式处理器测试失败: " . $e->getMessage() . "</p>\n";
        }
    }
}

// 运行测试
$test = new ComposerFixTest();
$test->runAllTests();
?>